import Vue from 'vue'
import Vuex from 'vuex'
import { getUserAPI } from '@/api/user.js'
// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  plugins: [createPersistedState()],
  state: {
    token: '',
    user: {}
  },
  mutations: {
    // 修改 token 值
    updateToken(state, token) {
      state.token = token
    },
    // 更新用户信息
    updateUser(state, obj) {
      state.user = obj
    }
  },
  actions: {
    async initUser(ctx) {
      const { data: res } = await getUserAPI()
      if (res.code === 0) {
        ctx.commit('updateUser', res.data)
      }
    }
  },
  modules: {
  }
})
